﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataParser;

namespace argusThreeAndAHalfMen
{
    public partial class frmAddProduct : Form
    {
        private Category newCategory = new Category(); //new instance of category to be added
        private Product newProduct = new Product(); //new instance of product to be added

        //pointers
        private readonly Branch branch;
        private frmBranchDetails branchDetails;

        //ADD PRODUCT FORM
        public frmAddProduct(Branch branch, frmBranchDetails branchDetails)
        {
            InitializeComponent();

            this.branch = branch; //initalize with branch
            this.branchDetails = branchDetails; //initalize with branch details
        }

        //ADD BUTTON CLICK EVENT
        private void btnAdd_Click(object sender, EventArgs e)
        {
            bool productExists = false; //set the boolean for if the product exists to false by default.
            bool categoryExists = false; //set the boolean for if the category exists to false by default.

            if (txtAddProductID.Text.Length == Convert.ToInt16(7)) //if the id is more or less than 7 characters, run the following.
            {
                try
                {
                    newProduct.ProductID = Convert.ToInt32(txtAddProductID.Text); //if valid, add.
                }
                catch
                {
                    MessageBox.Show("Please enter a valid 7 digit ID number.", "Invalid ID"); //the message shown to the user if input is wrong.
                    return;
                }
                newProduct.ProductName = txtAddProductName.Text; //add product name
                newProduct.ProductImagePath = imgProductImage.ImageLocation; //add image path.
                newProduct.ProductDesc = txtAddProductDetails.Text; //add product description
                try
                {
                    if (txtAddProductPrice.Text.Substring(0, 1) == "£") //substring to remove £ sign if the user enters the input as '£1.99"
                        newProduct.ProductPrice = Convert.ToSingle(txtAddProductPrice.Text.Substring(1)); 
                    else if (txtAddProductPrice.Text.Substring(0, 1) != "£") //if the input does not contain a £, run this.
                        newProduct.ProductPrice = Convert.ToSingle(txtAddProductPrice.Text); 
                }
                catch
                {
                    MessageBox.Show("Please enter a valid product price.", "Invalid price"); //messagebox to show user the error message.
                    return; //return error message.
                }

                try
                {
                    newProduct.ProductStockLevel = Convert.ToInt16(txtAddProductStock.Text); //add stock level from input.
                }
                catch
                {
                    MessageBox.Show("Please enter a valid stock level.", "Invalid stock level"); //if stock input is invalid, show this message.
                }

                string message = "Are you sure you want to add this product?"; //confirm if the user wants to add the product.
                string caption = "Add Product"; //this will show in the messagebox window.
                MessageBoxButtons buttons = MessageBoxButtons.YesNo; //yes and no option buttons which appear in the window.
                DialogResult result; //result is gathered from the code below.

                // Displays the MessageBox.
                result = MessageBox.Show(message, caption, buttons); //show the result with the message from above, caption, and buttons.

                if (result == System.Windows.Forms.DialogResult.Yes) //if the yes button is clicked, run the following.
                {
                    foreach (Category category in branch.Categories) //go through the categories in the specific branch category.
                    {
                        if (Convert.ToInt32(cboProductCategory.Text.Substring(cboProductCategory.Text.IndexOf(" (") + 2, 3)) == category.CategoryID) //if the category in the category combobox is equal to the selected category, run the following
                        {
                            categoryExists = true; //if the category exists, run the following.

                            foreach (Product product in category.ProductList) //go through each of the products in the categories product list and find the product.
                            {
                                if (newProduct.ProductID == product.ProductID) // if the product id matches the product id in the product array list, the already product exists in the branch.
                                    productExists = true; //the product exists.
                            }

                            if (productExists == false) //if the product does not exist, run the following.
                                category.ProductList.Add(newProduct); //if the product does not exist, add it to the product list.
                            else if (productExists == true) //if the product does exist run the following.
                            {
                                MessageBox.Show("This product already exists.", "Product Exists"); //if the product is already exists at the branch, show this message.
                                return;
                            }

                        }
                    }


                    if (categoryExists == false) //if the category does not exist in the branch, add the category to the product list with the product in the category.
                    {
                        newCategory.ProductList.Add(newProduct);
                        addCategory();
                        branch.Categories.Add(newCategory);
                    }

                    Parser.SaveBranchList("argus-BIG.txt"); //save to the default file located in the debug file.

                    branchDetails.refreshSearchList(); //refresh the search list
                    branchDetails.refreshCategories(); //refresh the categories in the combobox.
                    branchDetails.resetListboxSelection(); //reset the listbox selection.
                    
                    this.Close(); // Closes the parent form.

                }
            }

            else
                MessageBox.Show("Product ID must be a 7 digit number.", "Invalid input"); //the message shown to the user if input is wrong.
        }

        //CANCEL BUTTON CLICK EVENT
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close(); //close the form if cancel is clicked.
        }

        //ADD CATEGORY EVENT
        private Category addCategory()
        {
            if (cboProductCategory.Text == "Clothing (999)") //if this category is selected from combobox, run the following.
            {
                newCategory.CategoryName = "Clothing"; //add clothing as new category
                newCategory.CategoryID = 999; //give the following id to the category
            }

            else if (cboProductCategory.Text == "Garden & DIY (998)") //if this category is selected from combobox, run the following.
            {
                newCategory.CategoryName = "Garden & DIY"; //add garden & diy as new category
                newCategory.CategoryID = 998; //give the following id to the category
            }

            else if (cboProductCategory.Text == "Health & Personal Care (997)") //if this category is selected from combobox, run the following.
            {
                newCategory.CategoryName = "Health & Personal Care"; //add health & personal care as new category
                newCategory.CategoryID = 997; //give the following id to the category
            }

            else if (cboProductCategory.Text == "Home Entertainment (996)") //if this category is selected from combobox, run the following.
            {
                newCategory.CategoryName = "Home Entertainment"; //add home entertainment as new category
                newCategory.CategoryID = 996; //give the following id to the category
            }

            else if (cboProductCategory.Text == "Home & Furniture (995)") //if this category is selected from combobox, run the following.
            {
                newCategory.CategoryName = "Home & Furniture"; //add home & furniture as new category
                newCategory.CategoryID = 995; //give the following id to the category
            }

            else if (cboProductCategory.Text == "Jewellery & Watches (994)") //if this category is selected from combobox, run the following.
            {
                newCategory.CategoryName = "Jewellery & Watches"; //add jewellery & watches as new category
                newCategory.CategoryID = 994; //give the following id to the category
            }

            else if (cboProductCategory.Text == "Kitchen & Laundry (993)") //if this category is selected from combobox, run the following.
            {
                newCategory.CategoryName = "Kitchen & Laundry"; //add kitchen & laundry as new category
                newCategory.CategoryID = 993; //give the following id to the category
            }

            else if (cboProductCategory.Text == "Nursery (992)") //if this category is selected from combobox, run the following.
            {
                newCategory.CategoryName = "Nursery"; //add nursery as new category
                newCategory.CategoryID = 992; //give the following id to the category
            }

            else if (cboProductCategory.Text == "Toys & Games (991)") //if this category is selected from combobox, run the following.
            {
                newCategory.CategoryName = "Toys & Games"; //add toys & games as new category
                newCategory.CategoryID = 991; //give the following id to the category
            }

            else if (cboProductCategory.Text == "Sports & Leisure (990)") //if this category is selected from combobox, run the following.
            {
                newCategory.CategoryName = "Sports & Leisure"; //add sports & leisure as new category
                newCategory.CategoryID = 990; //give the following id to the category
            }

            return newCategory;
        }

        //ADD PRODUCT LOAD EVENT
        private void frmAddProduct_Load(object sender, EventArgs e)
        {
            cboProductCategory.Items.Add("Clothing (999)"); //add this category and following to combobox when loaded. 
            cboProductCategory.Items.Add("Garden & DIY (998)");
            cboProductCategory.Items.Add("Health & Personal Care (997)");
            cboProductCategory.Items.Add("Home Entertainment (996)");
            cboProductCategory.Items.Add("Home & Furniture (995)");
            cboProductCategory.Items.Add("Jewellery & Watches (994)");
            cboProductCategory.Items.Add("Kitchen & Laundry (993)");
            cboProductCategory.Items.Add("Nursery (992)");
            cboProductCategory.Items.Add("Toys & Games (991)");
            cboProductCategory.Items.Add("Sports & Leisure (990)");

            cboProductCategory.SelectedItem = cboProductCategory.Items[0]; //select the first item as default choice.
        }

        //TEXT BOX ADD PRODUCT ID LEAVE EVENT
        private void txtAddProductID_Leave(object sender, EventArgs e)
        {
            if (imgProductImage.ImageLocation == null) //if the image is not loaded, run the following.
            {
                imgProductImage.ImageLocation = @"C:/ArgusImages/" + txtAddProductID.Text + ".jpg"; //load images from the the path.
            }
        }

        //BTN BROWSE FILE CLICK EVENT
        private void btnBrowseFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog browseForImage = new OpenFileDialog(); //open browser window.
            browseForImage.Title = "Choose a product image"; //show this in window frame.
            browseForImage.InitialDirectory = @"c:\"; //load from this path.
            browseForImage.Filter = "All files (*.*)|*.*|All files (*.*)|*.*"; //filter by the '.'
            browseForImage.FilterIndex = 2; //filter index by 2
            browseForImage.RestoreDirectory = true; //restore directory is true if image is selected.
            if (browseForImage.ShowDialog() == DialogResult.OK) //if the image is selected, run the following.
            {
                imgProductImage.ImageLocation = browseForImage.FileName; //the image name is equal to the file name.
            }
        }
    }
}